//v.2.6 build 100722

/*
Copyright DHTMLX LTD. http://www.dhtmlx.com
You allowed to use this component or parts of it under GPL terms
To use it on other terms or get Professional edition of the component please contact us at sales@dhtmlx.com
*/
function dhtmlXGridFromTable(obj,init,noSetSizes){
	if(typeof(obj)!='object'){
		obj = document.getElementById(obj);
	}
	obj.className="";
    var w=document.createElement("DIV");
    w.setAttribute("width",obj.getAttribute("gridWidth")||obj.getAttribute("width")||(obj.offsetWidth?(obj.offsetWidth+"px"):0)||(window.getComputedStyle?window.getComputedStyle(obj,null)["width"]:(obj.currentStyle?obj.currentStyle["width"]:0)));
    w.setAttribute("height",obj.getAttribute("gridHeight")||(obj.offsetHeight?(obj.offsetHeight+"px"):0)||(window.getComputedStyle?window.getComputedStyle(obj,null)["height"]:(obj.currentStyle?obj.currentStyle["height"]:0)));    
    var mr=obj;
    var drag=obj.getAttribute("dragAndDrop");
    mr.parentNode.insertBefore(w,mr);
    var f=mr.getAttribute("name")||("name_"+(new Date()).valueOf());
    var windowf=new dhtmlXGridObject(w);
    window[f]=windowf; 
    var acs=mr.getAttribute("onbeforeinit");
   // var acs2=mr.getAttribute("oninit");
	if (acs){ eval(acs);}
	windowf.setImagePath(mr.getAttribute("imgpath")||"");
	if (init){ init(windowf);}
    var hrow=mr.rows[0];
    var za="";
    var zh = new Object();//为显示列而设,如果有多行没标题，需要使用&nbsp;的个数来区分
    var zb="";
    var zc="";
    var zd="";
    var ze="";
	var zf="";
	var zs="";
	var zr="";
	var zclmW = ""; 	//列最小宽度
	var width=0;
	var key="";
	var tooltips="";
	var tablewidth=mr.offsetWidth||(window.getComputedStyle ? window.getComputedStyle(mr, null)["width"] : (mr.currentStyle ? mr.currentStyle["width"] : 0));
    for (var i=0 , len = hrow.cells.length; i<len; i++){
    	zr = hrow.cells[i].getAttribute("colspan");
        za+=(za?",":"")+hrow.cells[i].innerHTML;
        key = (_isIE?hrow.cells[i].innerText:hrow.cells[i].textContent).replace( /(^[\t\r\n]*)|([\t\r\n]*$)/g, '' );
		if(!hrow.cells[i].getAttribute("title")){
			zh[key]=hrow.cells[i].innerHTML.replace(/<[^>]*>/g,"");
		}else{
			zh[key]=hrow.cells[i].getAttribute("title");
		}
        width=hrow.cells[i].getAttribute("width")||hrow.cells[i].offsetWidth||(window.getComputedStyle?window.getComputedStyle(hrow.cells[i],null)["width"]:(hrow.cells[i].currentStyle?hrow.cells[i].currentStyle["width"]:0));
        if((""+width).indexOf("%")>-1){
			width = tablewidth * parseInt(width) / 100;
		}
		if(zr && zr>1){
			width = width=="*"?width:(parseFloat(width)/parseFloat(zr));
    		for(var j=0;j<zr-1;j++){
    			za+=","+"#cspan";
    			zs +=", ";
    			zb+=","+width;
    			zc+=",left";
    			zd+=","+(hrow.cells[i].getAttribute("type")||"ed");
        		ze+=","+(hrow.cells[i].getAttribute("sort")||"");
    		}
    		zs +=",background:none;";
    		zf+=(zf?",":"")+hrow.cells[i].getAttribute("childs");
    		windowf.needToCookie=false;
    	}else{
    		zf+=(zf?",":"")+"#rspan";
    		zs+=(zs==""?" ":", ");
    		windowf.needToCookie=true;
    	}
        zb+=(zb?",":"")+(width=="*"?width:parseInt(width));
        zc+=(zc?",":"")+(hrow.cells[i].getAttribute("align")||"left");
        zd+=(zd?",":"")+(hrow.cells[i].getAttribute("type")||"ed");
        ze+=(ze?",":"")+(hrow.cells[i].getAttribute("sort")||"str");
        zclmW+=","+(hrow.cells[i].getAttribute("minWidth")||"");
        tooltips+=(tooltips?",":"")+(hrow.cells[i].getAttribute("tooltips")||"false");
    	var f_a=hrow.cells[i].getAttribute("format");
    	if (f_a)
    		if(hrow.cells[i].getAttribute("type").toLowerCase().indexOf("calendar")!=-1) 
    			windowf._dtmask=f_a;
    		else
    			windowf.setNumberFormat(f_a,i);
    }
	windowf.enableTooltips(tooltips);
	windowf.setHeader(za);
	windowf.setHeaderTitle(zh); 
	if(!windowf.needToCookie){windowf.attachHeader(zf,zs);}
	windowf.enableRowsHover(true,"tdover");
	windowf.setInitWidths(zb);
	windowf.setColAlign(zc);
	windowf.setColTypes(zd);
	windowf.setColSorting(ze);
	if (zclmW && zclmW.length>0)
		windowf.setColumnMinWidth(zclmW.substring(1));
	if (obj.getAttribute("gridHeight")=="auto")
    	windowf.enableAutoHeigth(true);
	if (obj.getAttribute("multiline")) windowf.enableMultiline(true);
	var lmn=mr.getAttribute("lightnavigation");
	if (lmn) windowf.enableLightMouseNavigation(lmn);
	var evr=mr.getAttribute("evenrow");
	var uevr=mr.getAttribute("unevenrow");
	if (evr||uevr) windowf.enableAlterCss(evr,uevr);
	if (drag) windowf.enableDragAndDrop(true);
    windowf.init();
    if (obj.getAttribute("split")) windowf.splitAt(obj.getAttribute("split"));
    //adding rows
   windowf._process_inner_html(mr,1);
    var trueHeaderCols = windowf.hdr.rows[1].cells;
	var sortStr,sortColEle,sortType='asc';
	for(var n=0;n<trueHeaderCols.length;n++)
	{
		sortStr = windowf.fldSort[trueHeaderCols[n]._cellIndex];
		if(sortStr=="")	//如果td没有设置sort,则默认为sort="str"
		{
			windowf.fldSort[trueHeaderCols[n]._cellIndex] = "str"
			continue;
		}
		if(windowf.fldSort[trueHeaderCols[n]._cellIndex]=="de-str")
		{
			windowf.fldSort[trueHeaderCols[n]._cellIndex] = "str";
			sortColEle = trueHeaderCols[n];
			sortType="desc";
			break;
		}
		if(windowf.fldSort[trueHeaderCols[n]._cellIndex]=="de-int")
		{
			windowf.fldSort[trueHeaderCols[n]._cellIndex] = "int";
			sortColEle = trueHeaderCols[n];
			sortType="desc";
			break;
		}
		if(windowf.fldSort[trueHeaderCols[n]._cellIndex]=="asc-str")
		{
			windowf.fldSort[trueHeaderCols[n]._cellIndex] = "str";
			sortColEle = trueHeaderCols[n];
			sortType="asc";
			break;
		}
		if(windowf.fldSort[trueHeaderCols[n]._cellIndex]=="asc-int")
		{
			windowf.fldSort[trueHeaderCols[n]._cellIndex] = "int";
			sortColEle = trueHeaderCols[n];
			sortType="asc";
			break;
		}
	}
	if(sortColEle)windowf.sortField(sortColEle._cellIndexS, false, sortColEle,sortType);
	//windowf.setSizes();
	//if (acs2) eval(acs2);            
	if (obj.parentNode && obj.parentNode.removeChild){
		obj.parentNode.removeChild(obj);
	}
	if (!noSetSizes)
		windowf.setSizes();
	//windowf.enableHeaderMenu(true);//头部是否允许右键菜单
	return windowf;
}
dhtmlXGridObject.prototype._process_html=function(xml){
	if (xml.tagName && xml.tagName == "TABLE") return this._process_inner_html(xml,0);
	var temp=document.createElement("DIV");
	temp.innerHTML=xml.xmlDoc.responseText;
	var mr = temp.getElementsByTagName("TABLE")[0];
	this._process_inner_html(mr,0);
}
dhtmlXGridObject.prototype._process_inner_html=function(mr,start){
	var n_l=mr.rows.length;
	for (var j=start; j<n_l; j++){
		var id=mr.rows[j].getAttribute("id")||j;
		this.rowsBuffer.push({ idd:id, data:mr.rows[j], _parser: this._process_html_row, _locator:this._get_html_data });
	}
	this.render_dataset();
	//this.setSizes();
}
   
dhtmlXGridObject.prototype._process_html_row=function(r,xml){
	var cellsCol = xml.getElementsByTagName('TD');
    var strAr = [];
    
	r._attrs=this._xml_attrs(xml);
	
	//load cell data
    for(var j=0;j<cellsCol.length;j++){
    	var cellVal=cellsCol[j];
        var exc=cellVal.getAttribute("type");
        if (r.childNodes[j]){
        	if (exc)
        		r.childNodes[j]._cellType=exc;
       		r.childNodes[j]._attrs=this._xml_attrs(cellsCol[j]);
   		}
       
		if (cellVal.firstChild)
		    strAr.push(cellVal.innerHTML);
		else strAr.push("");
        
        if (cellVal.colSpan>1){
            r.childNodes[j]._attrs["colspan"]=cellVal.colSpan;		
            for (var k=1; k<cellVal.colSpan; k++){
                strAr.push("")
            }
        }
		
}
	for(j<cellsCol.length; j<r.childNodes.length; j++)
        r.childNodes[j]._attrs={};

        
    //back to common code
	this._fillRow(r,(this._c_order?this._swapColumns(strAr):strAr));
    return r;
}
dhtmlXGridObject.prototype._get_html_data=function(data,ind){
	data=data.firstChild;
	while (true){
		if (!data) return "";
		if (data.tagName=="TD") ind--;
		if (ind<0) break;
		data=data.nextSibling;
	}
  return (data.firstChild?data.firstChild.data:"");
}



dhtmlxEvent(window,"load",function(){
    var z=document.getElementsByTagName("table");
    for (var a=0; a<z.length; a++)
        if (z[a].className=="dhtmlxGrid"){
            dhtmlXGridFromTable(z[a]);
            //we have found IT!
        }
});


//(c)dhtmlx ltd. www.dhtmlx.com
